<?php
class ControllerProductTryon extends Controller {

    /**
     * 眼镜试戴
     *
     * @throws Exception
     */
	public function glass()
    {
        $data['models'] = $this->getProductModels();

        $this->response->setOutput($this->load->view('tryon/glass', $data));
	}

    /**
     * 返回眼镜 json
     */
    public function glassJson()
    {
        $this->responseOK([
            'models' => $this->getProductModels()
        ]);
	}

    public function getProductModels()
    {
        /** @var ModelCatalogTryonModel $modelTryonModel */
        $modelTryonModel = $this->loadModel('catalog/tryon_model');

        $models = $modelTryonModel->findPcGlassesByProductId($this->requestGet('product_id'));

        return array_map(function ($model) {
            $model['preview'] = '/' . $this->url->imageLink($model['preview']);
            $model['url'] = '/' . $this->url->imageLink($model['model']);
            $model['name'] = $model['code'];

            return $model;
        }, $models);
    }
}
